/*
Chris Elrod
10/27/2008
CSci 252
Prof. Hasan

Project 2 - EJB

Reference: 
    BEA trader example: BEA 10.3 HOME\wlserver_10.3\samples\server\examples\src\examples\ejb\ejb20\basic\statefulSession
*/

package examples.ejb.ejb20.basic.Project2;

import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import weblogic.ejbgen.*;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.*;
import java.util.Hashtable;


/**
 * SecurityBean is a stateless Session Bean. This bean illustrates:
 * <ul>
 * <li> No persistence of state between calls to the Session Bean
 * <li> Looking up values from the Environment
 * </ul>
 *
 */


@FileGeneration(remoteClass = Constants.Bool.TRUE,
                localHome = Constants.Bool.FALSE,
                remoteHome = Constants.Bool.TRUE,
                remoteClassName = "Security",
                remoteHomeName = "SecurityHome",
                localClass = Constants.Bool.FALSE)
@JarSettings(ejbClientJar = "Project2_client.jar")
@JndiName(remote = "Project2-Security-SecurityHome")
@Session(idleTimeoutSeconds = "600",
         maxBeansInCache = "1000",
         transTimeoutSeconds = "0",
         type = Session.SessionType.STATEFUL,
         defaultTransaction = Constants.TransactionAttribute.REQUIRED,
         ejbName = "statelessSession_Security",
         enableCallByReference = Constants.Bool.TRUE)

public class SecurityBean implements SessionBean {

  private SessionContext ctx;
  private Hashtable table = new Hashtable();

  public SecurityBean(){}

  private void log(String s) {
    System.out.println(s);
  } 

  
  /**
   * This method is required by the EJB Specification,
   * but is not used by this example.
   */
  public void ejbActivate() {
    log("ejbActivate called");
  }


  /**
   * This method is required by the EJB Specification,
   * but is not used by this example.
   */
  public void ejbRemove() {
    log("ejbRemove called");
  }


  /**
   * This method is required by the EJB Specification,
   * but is not used by this example.
   */
  public void ejbPassivate() {
    log("ejbPassivate called");
  }


  /**
   * 
   * This method is required by the EJB Specification,
   * but is not used by this example.
   *
   * Sets the session context.
   *
   * @param ctx               SessionContext Context for session
   */
  public void setSessionContext(SessionContext ctx) {
    log("setSessionContext called");
    this.ctx = ctx;
  }


  /**
   * 
   *
   * This method corresponds to the create method in the home interface
   * "SecurityHome.java".
   * The parameter sets of the two methods are identical. When the client calls
   * <code>SecurityHome.create()</code>, the container allocates an instance of 
   * the EJBean and calls <code>ejbCreate()</code>.
   *
   * @exception               javax.ejb.CreateException if there is
   *                          a communications or systems failure
   * @see                     examples.ejb.ejb20.basic.statelessSession.Security
   */

  public void ejbCreate () throws CreateException {
    log("ejbCreate called");

    try {
      InitialContext ic = new InitialContext();
    } catch (NamingException ne) {
      throw new CreateException("Failed to find environment value "+ne);
    }
  }


  /*Validates parms user id and password.  Adds a user and returns success.*/
  @RemoteMethod()
  
  public boolean createUser(String uid, String password){

    log("In createUser(uid,password) and uid = " + uid + " and password= " + password);

    boolean b = Pattern.matches("[A-Za-z0-9]*", uid);

    if (b == false){ return false; }

    boolean b2 = Pattern.matches("[A-Za-z0-9]*", password);

    if (b2 == false){ return false; }

    if ( uid.length() < 6 || uid.length() > 15 ) { return false; }

    if ( password.length() < 6 || password.length() > 15 ) { return false; }           
    
    table.put(uid,password);

    return true;    
  }


  /*Returns an address based on match between the agrument uid and the address.txt file.*/ 
  @RemoteMethod()
  
  public boolean authenticate(String uid, String password){

    if(table.get(uid).toString().equals(password)){return true;}
    
    return false; 
  }

}








